package com.marisfrolg.base;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.marisfrolg.util.EmptyControll;
import com.marisfrolg.util.HadwareControll;
import com.marisfrolg.util.ReadCodeController;
import com.marisfrolg.util.UrovoScanControll;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;

public class BaseApplication extends Application {
	private Map<Object, Object> gValue=new HashMap<Object, Object>();
	private ReadCodeController rcc;
	private DbUtils db;
	private final static String APPVERSION="APPVERSION";
	
	public ReadCodeController getReadCodeController(){
		return rcc;
	}
	@Override
	public void onCreate() {
//		DbUtils.create(this, Environment.getExternalStorageDirectory().getPath()+"/code/", "code.db");
//		DbUtils db=DbUtils.create(this);
//		CodeMapper cm=new CodeMapper();
//		cm.setOldcode("6916999000713");
//		cm.setNewcode("查到新编码OOXX");
//		try {
//			db.saveBindingId(cm);
//		} catch (DbException e) {
//			e.printStackTrace();
//		}
		checkAndUpdateDB();
		if(android.os.Build.MANUFACTURER.toUpperCase(Locale.US).contains("ALPS")){
			rcc=new HadwareControll();
		}else if(android.os.Build.MANUFACTURER.toUpperCase(Locale.US).contains("UROVO")){
			rcc=new UrovoScanControll(this);
		}else{
			rcc=new HadwareControll();
			//rcc=new EmptyControll();
		}
	}
	
	private void checkAndUpdateDB() {
		try {
			db=DbUtils.create(this,Environment.getExternalStorageDirectory().getPath() + "/storage/","storage.db");
			PackageManager manager = this.getPackageManager();
			PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
			String appVersion = info.versionName; // 版本名
			SharedPreferences sp=getSharedPreferences(APPVERSION, MODE_PRIVATE);
			String storeV=sp.getString("v", "0");
			if(!storeV.equals(appVersion)){
				db.dropDb();
				Editor editor=sp.edit();
				editor.putString("v", appVersion);
				editor.commit();
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		} catch (DbException e) {
			e.printStackTrace();
		}
	}
	public Map<Object, Object> getgValue() {
		return gValue;
	}

	public void setgValue(Map<Object, Object> gValue) {
		this.gValue = gValue;
	}
	
	
	

}
